package com.yrline.pay.services;

import java.util.List;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import com.yrline.pay.proxy.PayInvoke;
import com.yrline.pay.proxy.request.CommonRequest;
import com.yrline.pay.proxy.request.QueryParam;
import com.yrline.pay.proxy.response.BankDetails;
import com.yrline.pay.proxy.response.BankResponse;
import com.yrline.pay.util.concurrent.ExecutorManager;

@Service
public class BankInfoServices {

	@Autowired
    public  RedisTemplate<String, Object> redistTemplate;
	
	private static String BANK_key = "BANK_INFO";
	
	@Autowired
	PayInvoke payInvoke;
	

	public void loadBank(){
		
				
				redistTemplate.delete(BANK_key);
				CommonRequest _req = new CommonRequest();
				_req.setOperationType("query.bankQuery");
				_req.setRequestObj(null);
				BankResponse response =(BankResponse)payInvoke.invoke(_req, BankResponse.class);
				if(response !=null){
					for(BankDetails d:response.getBanks()){
						redistTemplate.opsForList().leftPush(BANK_key, d);
					}
				}
				
			
		
		
	}
	
	
	public List<BankDetails> selectAll(){
		if(!redistTemplate.hasKey(BANK_key)){
			loadBank();
		}
		return (List<BankDetails> )(List)this.redistTemplate.opsForList().range(BANK_key, 0, -1);
	}
}
